package com.stars.home.activity

import android.graphics.Color
import android.net.http.SslError
import android.os.Bundle
import android.util.Log
import android.webkit.*
import com.stars.home.base.MBActivity
import com.stars.home.databinding.ActivityArticleDetailBinding
import com.stars.home.utils.WebViewUtils
import com.stars.home.vmodel.ArticleDetailVModel


/**
 *
 *on 2020/9/28
 */
class ArticleDetailActivity : MBActivity<ArticleDetailVModel, ActivityArticleDetailBinding>() {


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        var type = intent?.extras?.getString("type")

        if (type.equals("1")) {
            titleBar()?.tvTitle?.text = "公司介绍"
        }
        if (type.equals("2")) {
            titleBar()?.tvTitle?.text = "平台协议"
        }
        if (type.equals("3")) {
            titleBar()?.tvTitle?.text = "用户协议"
        }
        if (type.equals("4")) {
            titleBar()?.tvTitle?.text = "隐私政策"
        }
        if (type.equals("5")) {
            titleBar()?.tvTitle?.text = "交易规则"
        }
        if (type.equals("6")) {
            titleBar()?.tvTitle?.text = "充值规则"
        }
        if (type.equals("7")) {
            titleBar()?.tvTitle?.text = "法律声明"
        }
        if (type.equals("8")) {
            titleBar()?.tvTitle?.text = "用户注销协议"
        }
        initWeb() //初始化

        vm.getInfo()

        vm.bean.observe(this) {

            when (type) {
                "1" -> {
                    setContent(it.platformIntroduce)
                }

                "2" -> {
                    setContent(it.platformAgreement)
                }
                "3" -> {
                    setContent(it.userAgreement)

                }
                "4" -> {
                    setContent(it.privacyPolicy)
                }
                "5" -> {
                    setContent(it.transactionRules)
                }
                "6" -> {
                    setContent(it.rechargingRules)
                }
                "7" -> {
                    setContent(it.legalStatement)
                }
                "8" -> {
                    setContent(it.accountCancellationAgreement)
                }
            }

        }


    }

    fun setContent(content: String) {
        binding.webContent.loadDataWithBaseURL(
            null,
            WebViewUtils.getHtmlData2(content),
            "text/html",
            "utf-8",
            null
        )

    }


    private fun initWeb() {
        if (null != binding.webContent) {
            val webSettings: WebSettings = binding.webContent.settings
            webSettings.javaScriptEnabled = true
            webSettings.domStorageEnabled = true
            webSettings.setAppCacheEnabled(true)
            webSettings.pluginState = WebSettings.PluginState.ON
            webSettings.useWideViewPort = true // 关键点

            webSettings.allowFileAccess = true // 允许访问文件

            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE //没有缓存LOAD_NO_CACHE
            webSettings.domStorageEnabled = true;
            webSettings.databaseEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.displayZoomControls = false
            webSettings.loadWithOverviewMode = true
            webSettings.mediaPlaybackRequiresUserGesture = false


//            webSettings.textZoom = 250
            // 支持通过js打开新的窗口
            binding.webContent.settings.javaScriptCanOpenWindowsAutomatically = true

            binding.webContent.webViewClient = object : WebViewClient() {

                override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
                   // super.onReceivedSslError(view, handler, error)

                    Log.e("-web-", "error=: "+error )
                    handler?.proceed()

                }


                override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
                    super.onReceivedError(view, request, error)

                    Log.e("-web-", "error=: "+error )
                }

            }


        }
    }


    override fun binding(): ActivityArticleDetailBinding {
        return ActivityArticleDetailBinding.inflate(layoutInflater)
    }

    override fun vmClass(): Class<ArticleDetailVModel> {
        return ArticleDetailVModel::class.java
    }

    override fun title(): String? {
        return "详情"
    }

    override val isRxbus: Boolean
        get() = true
}
